package com.braveman.im.ui.weiget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;

import com.braveman.im.R;
import com.braveman.im.bean.FriendGroupNameBean;
import com.braveman.im.manager.ApiManager;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.utils.UIUtils;

import de.greenrobot.event.EventBus;
import razerdp.basepopup.BasePopupWindow;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by 大灯泡 on 2016/1/18.
 * 带输入法的popup
 */
public class CommentAddPopup extends BasePopupWindow implements View.OnClickListener {
    private Button mCancelButton;
    private Button mCompeleteButton;
    private EditText mInputEdittext;
    private String beMentionedId = "";
    private String momentId;

    public CommentAddPopup(Activity context, String momentId, String beMentionedId) {
        super(context);
        mCancelButton = (Button) findViewById(R.id.btn_cancel);
        mCompeleteButton = (Button) findViewById(R.id.btn_Compelete);
        mInputEdittext = (EditText) findViewById(R.id.ed_input);
        this.momentId = momentId;
        if (!TextUtils.isEmpty(beMentionedId)) {
            this.beMentionedId = beMentionedId;
        }
        setAutoShowInputMethod(true);
        bindEvent();
    }

    @Override
    protected Animation initShowAnimation() {
        return null;
    }

    private void bindEvent() {
        mCancelButton.setOnClickListener(this);
        mCompeleteButton.setOnClickListener(this);
    }

    //=============================================================super methods


    @Override
    public Animator initShowAnimator() {
        return getDefaultSlideFromBottomAnimationSet();
    }

    @Override
    public EditText getInputView() {
        return mInputEdittext;
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.popup_input, null);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }

    @Override
    public Animator initExitAnimator() {
        AnimatorSet set = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            set = new AnimatorSet();
            if (initAnimaView() != null) {
                set.playTogether(
                        ObjectAnimator.ofFloat(initAnimaView(), "translationY", 0, 250).setDuration(400),
                        ObjectAnimator.ofFloat(initAnimaView(), "alpha", 1, 0.4f).setDuration(250 * 3 / 2));
            }
        }
        return set;
    }

    //=============================================================click event
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_Compelete:
                FriendGroupNameBean friendGroupNameBean = new FriendGroupNameBean();
                String name = mInputEdittext.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    UIUtils.showToast("评论不能为空");
                    return;
                }
                friendGroupNameBean.name = name;
                ApiManager.getApi().addFriendGroupName(friendGroupNameBean).enqueue(new NetCallBack<FriendGroupNameBean>() {
                    @Override
                    protected void onSuccess(Call<FriendGroupNameBean> call, Response<FriendGroupNameBean> response) {
                        if (response != null) {
                            UIUtils.showToast("添加成功");
                            EventBus.getDefault().post(new EventCenter(EventCode.EVENT_ADD_GROUP_NAME));
                        }
                    }
                });

                dismiss();
                break;
            default:
                break;
        }

    }
}
